// list-openid-uses.js

var ListOpenIDUses = {};

ListOpenIDUses.load = function() {
    var name = window.arguments[0].name;
    var exp = document.getElementById("list-open-id-uses-description");
    exp.value = exp.value + " " + name;
    ListOpenIDUses.refresh();
};

ListOpenIDUses.unload = function() {
    // do nothing
};

ListOpenIDUses.refresh = function() {
    var rowid = window.arguments[0].id
    var list = document.getElementById("openid-sites");
    for (var i = list.getRowCount(); i > 0; i--) {
        list.removeItemAt(i-1);
    }
    var uses = AppalachianDB.listUses(rowid);
    for (var i = 0; i < uses.length; i++) {
        var use = uses[i];
        var item = document.createElement("listitem");
        item.setAttribute("value", use["id"]);

        var listcell;

        listcell = document.createElement("listcell");
        listcell.setAttribute("label", use["url"]);
        item.appendChild(listcell);
        item.ondblclick = function() {
            var par = window.opener;
            while (par.opener) { par = par.opener; }
            par.getBrowser().selectedTab = par.getBrowser().addTab(this.firstChild.getAttribute("label"));
        };
        list.appendChild(item);
    }
};

ListOpenIDUses.current = function() {
    var list = document.getElementById("openid-sites");
    var item = list.selectedItem;
    return item.value;
};

ListOpenIDUses.delete = function() {
    var id = ListOpenIDUses.current();
    var bundle = document.getElementById("list-openid-uses-bundle");
    var confmsg = bundle.getString("delconfirm");
    if (confirm(confmsg)) {
        AppalachianDB.deleteUse(id);
        ListOpenIDUses.refresh();
        ListOpenIDUses.unselect();
    }
};

ListOpenIDUses.selected = function() {
    document.getElementById("list-openid-uses-delete-button").disabled = false;
}

ListOpenIDUses.unselect = function() {
    document.getElementById("list-openid-uses-delete-button").disabled = true;
}

ListOpenIDUses.close = function() {
    window.close();
};
